package com.tanchengjin.admin.modules.blog.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.tanchengjin.admin.modules.blog.service.ArticleService;
import com.tanchengjin.admin.modules.blog.utils.pagination.Pagination;
import com.tanchengjin.admin.modules.cms.mapper.ArticleMapper;
import com.tanchengjin.admin.modules.cms.pojo.Article;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class ArticleServiceImpl implements ArticleService {
    @Autowired
    private ArticleMapper articleMapper;

    @Value(value = "${blog.article.click-point:1}")
    public String clickPoint;

    @Override
    public List<Article> getAll() {
        return articleMapper.getAll();
    }

    @Override
    public Article findOne(long id) {
        Article article = articleMapper.selectByPrimaryKey(id);
        if (article != null) {
            //访问量+1
            Article newArticle = new Article();
            newArticle.setId(article.getId());
            newArticle.setClick(article.getClick() + Integer.parseInt(clickPoint));
            articleMapper.updateByPrimaryKeySelective(newArticle);
        }
        return article;
    }

    @Override
    public int delete(long[] id) {
        return 0;
    }

    @Override
    public int update(Article article) {
        return 0;
    }

    /**
     * @param page
     * @param size
     * @return
     * @see #getArticleListByPagination(int page, int size)
     */
    @Override
    @Deprecated
    public Pagination<Article> pagination(int page, int size) {
        if (page <= 1) page = 0;
        if (size <= 0) size = 10;
        List<Article> articleList = articleMapper.pagination(page, size);
        long count = articleMapper.count();
        Pagination<Article> articlePagination = new Pagination<>();

        articlePagination.setPage(page);
        articlePagination.setSize(size);
        articlePagination.setRows(articleList);
        articlePagination.setTotalSize(count);
        int num = (int) Math.ceil(count / size);
        articlePagination.setTotalSize(num);
        if (page >= 1) {
            articlePagination.setCurrentPage(page - 1);
        } else {
            articlePagination.setCurrentPage(page);
        }
        return articlePagination;
    }

    @Override
    public PageInfo<Article> getArticleListByPagination(int page, int size) {
        PageHelper.startPage(page, size);
        List<Article> articleList = articleMapper.getAll();
        PageInfo<Article> articlePageInfo = new PageInfo<>(articleList);
        return articlePageInfo;
    }

    /**
     * 通过分类获取文章列表
     *
     * @param cid
     * @param page
     * @param size
     * @return
     */
    @Override
    public PageInfo<Article> getArticleListByCategoryId(int cid, int page, int size) {
        PageHelper.startPage(page, size);
        List<Article> articleList = articleMapper.getAllByCategoryId(cid);
        PageInfo<Article> articlePageInfo = new PageInfo<>(articleList);
        return articlePageInfo;
    }

    /**
     * 通过标签获取文章列表
     *
     * @param tag
     * @param page
     * @param size
     * @return
     */
    @Override
    public PageInfo<Article> getArticleListByTag(String tag, int page, int size) {
        PageHelper.startPage(page, size);
        List<Article> articleList = articleMapper.getByTag(tag);
        PageInfo<Article> articlePageInfo = new PageInfo<>(articleList);
        return articlePageInfo;
    }

    /**
     * 首页搜索
     *
     * @param page
     * @param size
     * @param q
     * @return
     */
    @Override
    public PageInfo<Article> getArticleListByPaginationWithSearch(int page, int size, String q) {
        PageHelper.startPage(page, size);
        List<Article> articleList = articleMapper.getBySearch(page, size, q);
        PageInfo<Article> articlePageInfo = new PageInfo<>(articleList);
        return articlePageInfo;
    }
}
